import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:wanchong/path.dart';
import 'package:wanchong/widget/app_bar.dart';
import 'package:wanchong/api/user.dart';
import 'package:wanchong/bean/login_go_entity.dart';
import 'package:wanchong/utils/common.dart';
import 'package:wanchong/bean/set_password_entity.dart';
class SetPwd extends StatefulWidget {
  @override
  _SetPwdState createState() => _SetPwdState();
}

class _SetPwdState extends State<SetPwd> {
  TextEditingController _controller = TextEditingController();
  TextEditingController _rep_controller = TextEditingController();

  bool _pwd_ob = true;
  bool _rep_pwd_ob = true;
  //是否显示删除的图标
  bool _rm_pwd = false;
  //是否显示删除的图标
  bool _rm_rep_pwd = false;
  bool same = false;
  bool started = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomPadding: false,
      appBar: MyAppBar(
        color: Colors.transparent,
        child: Align(
          alignment: Alignment.centerRight,
          child: GestureDetector(
            onTap: () {
              // print("密码登录");
              ///下次再说
              _goHome();
            },
            child: Container(
              padding: EdgeInsets.only(right: 20.0),
              child: Text(
                "下次再说",
                style: TextStyle(color: Color(0xFF3C4450), fontSize: 15.0),
              ),
              // color: Colors.red,
            ),
          ),
        ),
      ),
      body: Container(
        margin: EdgeInsets.only(left: 46.0, right: 46.0),
        child: Column(
          children: <Widget>[
            Container(
              width: double.infinity,
              // color: Colors.red,
              margin: EdgeInsets.only(top: 48.0),
              child: Text(
                "设置密码",
                style: TextStyle(
                    fontSize: 28.0,
                    color: Color(0xFF3C4450),
                    fontWeight: FontWeight.w700),
              ),
            ),
            Container(
              width: double.infinity,
              margin: EdgeInsets.only(top: 10.0),
              child: Text(
                "密码长度 8~16位，需包含数字、字母、字符至 少2种或以上元素",
                style: TextStyle(
                  fontSize: 14.0,
                  color: Color(0xFF647280),
                ),
              ),
            ),
            // Container(
            //   // height: ,
            //   child: ,
            // )
            SizedBox(
              height: 40.0,
            ),
            _pwd("请输入密码", _pwd_ob, _rm_pwd, _controller, () {
              setState(() {
                _pwd_ob = !_pwd_ob;
              });
            }, (text) {
              setState(() {
                _rm_pwd = text.length > 0;
                same = text == _rep_controller.text;
              });
            }),
            Divider(
              color: Color(0xFFDDDDDD),
              height: 1.0,
            ),
            SizedBox(
              height: 40.0,
            ),
            _pwd("请再次输入密码", _rep_pwd_ob, _rm_rep_pwd, _rep_controller, () {
              setState(() {
                _rep_pwd_ob = !_rep_pwd_ob;
              });
            }, (text) {
              setState(() {
                _rm_rep_pwd = text.length > 0;
                same = _controller.text == text;
              });
            }),
            Divider(
              color: Color(0xFFDDDDDD),
              height: 1.0,
            ),
            SizedBox(
              height: 40.0,
            ),
            GestureDetector(
              onTap: () {
                ///进入主页面
                ///
                _startMain();
              },
              child: ClipRRect(
                borderRadius: BorderRadius.circular(25.0),
                child: Container(
                  alignment: Alignment.center,
                  child: Text(
                    "确认",
                    style: TextStyle(color: Color(0xFFFFFFFF), fontSize: 18.0),
                  ),
                  color: same ? Color(0xFFFFCC33) : Color(0xFFFFE599),
                  height: 50.0,
                ),
              ),
            ),
            SizedBox(
              height: 30.0,
            ),
            Container(
              alignment: Alignment.center,
              child: Text(
                "可以在个人中心中设置或修改",
                style: TextStyle(
                  color: Color(0xFFB4BEC6),
                  fontSize: 12.0,
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _pwd(title, obscure, r, controller, call, rm) {
    return Container(
      child: Row(
        // mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Expanded(
            child: TextField(
              controller: controller,
              maxLength: 16,
              style: TextStyle(color: Color(0xFF424D59), fontSize: 24.0),
              keyboardType: TextInputType.text,
              obscureText: obscure,
              decoration: InputDecoration(
                counterText: "",
                contentPadding: const EdgeInsets.symmetric(vertical: 4.0),
                border: InputBorder.none,
                hintText: "请输入密码",
                hintStyle: TextStyle(color: Color(0xFFDBDBDB), fontSize: 18.0),
              ),
              onChanged: (text) {
                rm(text);
              },
            ),
          ),
          r
              ? GestureDetector(
                  onTap: () {
                    setState(() {
                      controller.text = "";
                      r = false;
                    }); 
                  },
                  child: Container(
                    margin: EdgeInsets.only(right: 14.0),  
                    width: 20.0,
                    child:
                        Image.asset("assets/login/login_input_icon_delete.png"),
                  ),
                )
              : Container(),
          GestureDetector(
            onTap: () {
              call();
            },
            child: Container(
              width: 20.0,
              child: obscure
                  ? Image.asset("assets/login/login_input_icon_eye_a.png")
                  : Image.asset("assets/login/login_input_icon_eye_b.png"),
            ),
          )
        ],
      ),
    );
  }

  void _startMain() {
    if (started) return;
    started = true;
    RegExp exp = RegExp(r'^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$');
    bool matched = exp.hasMatch(_controller.text);
    if (!matched) {
      Fluttertoast.showToast(
        msg: "密码格式不正确,请重新输入",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIos: 1,
        fontSize: 16.0,
      );
      started = false;
      return;
    }
    pswSoin(_rep_controller.text, _controller.text).listen((SetPasswordEntity data) {
       print(data);
      if (data.code == 200) {
        started = false;
        alert('设置成功');
        Future.delayed(Duration(seconds: 1), () {
          Navigator.pushNamedAndRemoveUntil(
              context, MeansPath, (Route<dynamic> route) => false);
        });
      }
    }).onError((err) {
      started = false;
      alert(err.message);
    });
  }

  void _goHome() {
    // Navigator.pushNamedAndRemoveUntil(context, HomePath, (Route<dynamic> route) => false);
    Navigator.pushNamedAndRemoveUntil(
        context, MeansPath, (Route<dynamic> route) => false);
  }
}











// import 'package:flutter/material.dart';
// import 'package:fluttertoast/fluttertoast.dart';
// import 'package:wanchong/path.dart';
// import 'package:wanchong/widget/app_bar.dart';
// import 'package:wanchong/api/user.dart';
// import 'package:wanchong/bean/login_entity.dart';
// import 'package:wanchong/utils/common.dart';

// class SetPwd extends StatefulWidget {
//   @override
//   _SetPwdState createState() => _SetPwdState();
// }

// class _SetPwdState extends State<SetPwd> {
//   TextEditingController _controller = TextEditingController();
//   TextEditingController _rep_controller = TextEditingController();

//   bool _pwd_ob = true;
//   bool _rep_pwd_ob = true;
//   //是否显示删除的图标
//   bool _rm_pwd = false;
//   //是否显示删除的图标
//   bool _rm_rep_pwd = false;
//   bool same = false;
//   bool started = false;
//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       resizeToAvoidBottomPadding: false,
//       appBar: MyAppBar(
//         color: Colors.transparent,
//         child: Align(
//           alignment: Alignment.centerRight,
//           child: GestureDetector(
//             onTap: () {
//               // print("密码登录");
//               ///下次再说
//               _goHome();
//             },
//             child: Container(
//               padding: EdgeInsets.only(right: 20.0),
//               child: Text(
//                 "下次再说",
//                 style: TextStyle(color: Color(0xFF3C4450), fontSize: 15.0),
//               ),
//               // color: Colors.red,
//             ),
//           ),
//         ),
//       ),
//       body: Container(
//         margin: EdgeInsets.only(left: 46.0, right: 46.0),
//         child: Column(
//           children: <Widget>[
//             Container(
//               width: double.infinity,
//               // color: Colors.red,
//               margin: EdgeInsets.only(top: 48.0),
//               child: Text(
//                 "设置密码",
//                 style: TextStyle(
//                     fontSize: 28.0,
//                     color: Color(0xFF3C4450),
//                     fontWeight: FontWeight.w700),
//               ),
//             ),
//             Container(
//               width: double.infinity,
//               margin: EdgeInsets.only(top: 10.0),
//               child: Text(
//                 "密码长度 8~16位，需包含数字、字母、字符至 少2种或以上元素",
//                 style: TextStyle(
//                   fontSize: 14.0,
//                   color: Color(0xFF647280),
//                 ),
//               ),
//             ),
//             // Container(
//             //   // height: ,
//             //   child: ,
//             // )
//             SizedBox(
//               height: 40.0,
//             ),
//             _pwd("请输入密码", _pwd_ob, _rm_pwd, _controller, () {
//               setState(() {
//                 _pwd_ob = !_pwd_ob;
//               });
//             }, (text) {
//               setState(() {
//                 _rm_pwd = text.length > 0;
//                 same = text == _rep_controller.text;
//               });
//             }),
//             Divider(
//               color: Color(0xFFDDDDDD),
//               height: 1.0,
//             ),
//             SizedBox(
//               height: 40.0,
//             ),
//             _pwd("请再次输入密码", _rep_pwd_ob, _rm_rep_pwd, _rep_controller, () {
//               setState(() {
//                 _rep_pwd_ob = !_rep_pwd_ob;
//               });
//             }, (text) {
//               setState(() {
//                 _rm_rep_pwd = text.length > 0;
//                 same = _controller.text == text;
//               });
//             }),
//             Divider(
//               color: Color(0xFFDDDDDD),
//               height: 1.0,
//             ),
//             SizedBox(
//               height: 40.0,
//             ),
//             GestureDetector(
//               onTap: () {
//                 ///进入主页面
//                 ///
//                 _startMain();
//               },
//               child: ClipRRect(
//                 borderRadius: BorderRadius.circular(25.0),
//                 child: Container(
//                   alignment: Alignment.center,
//                   child: Text(
//                     "确认",
//                     style: TextStyle(color: Color(0xFFFFFFFF), fontSize: 18.0),
//                   ),
//                   color: same ? Color(0xFFFFCC33) : Color(0xFFFFE599),
//                   height: 50.0,
//                 ),
//               ),
//             ),
//             SizedBox(
//               height: 30.0,
//             ),
//             Container(
//               alignment: Alignment.center,
//               child: Text( 
//                 "可以在个人中心中设置或修改",
//                 style: TextStyle(
//                   color: Color(0xFFB4BEC6),
//                   fontSize: 12.0,
//                 ),
//               ),
//             )
//           ],
//         ),
//       ),
//     );
//   }

//   Widget _pwd(title, obscure, r, controller, call, rm) {
//     return Container(
//       child: Row(
//         // mainAxisAlignment: MainAxisAlignment.center,
//         children: <Widget>[
//           Expanded(
//             child: TextField(
//               controller: controller,
//               maxLength: 16,
//               style: TextStyle(color: Color(0xFF424D59), fontSize: 24.0),
//               keyboardType: TextInputType.text,
//               obscureText: obscure,
//               decoration: InputDecoration(
//                 counterText: "",
//                 contentPadding: const EdgeInsets.symmetric(vertical: 4.0),
//                 border: InputBorder.none,
//                 hintText: "请输入密码",
//                 hintStyle: TextStyle(color: Color(0xFFDBDBDB), fontSize: 18.0),
//               ),
//               onChanged: (text) {
//                 rm(text);
//               },
//             ),
//           ),
//           r
//               ? GestureDetector(
//                   onTap: () {
//                     setState(() {
//                       controller.text = "";
//                       r = false;
//                     });
//                   },
//                   child: Container(
//                     margin: EdgeInsets.only(right: 14.0),
//                     width: 20.0,
//                     child:
//                         Image.asset("assets/login/login_input_icon_delete.png"),
//                   ),
//                 )
//               : Container(),
//           GestureDetector(
//             onTap: () {
//               call();
//             },
//             child: Container(
//               width: 20.0,
//               child: obscure
//                   ? Image.asset("assets/login/login_input_icon_eye_a.png")
//                   : Image.asset("assets/login/login_input_icon_eye_b.png"),
//             ),
//           )
//         ],
//       ),
//     );
//   }

//   void _startMain() {
//     if (started) return;
//     started = true;
//     RegExp exp = RegExp(r'^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$');
//     bool matched = exp.hasMatch(_controller.text);
//     // print(matched);
//     if (!matched) {
//       started = false;
//       Fluttertoast.showToast(
//         msg: "密码格式不正确,请重新输入",
//         toastLength: Toast.LENGTH_SHORT,
//         gravity: ToastGravity.CENTER,
//         timeInSecForIos: 1,
//         // backgroundColor: Colors.red,
//         // textColor: Colors.white,
//         fontSize: 16.0,
//       );
//       return;
//     }
//     pswSoin(_rep_controller.text, _controller.text).listen((LoginEntity data) {
//       started = false;
//       if (data.code == 200) {
//         alert('设置成功');
//         Future.delayed(Duration(seconds: 1), () {
          
//            Navigator.pushNamedAndRemoveUntil(
//                context, HomePath, (Route<dynamic> route) => false);
//         });
//       }
//     }).onError((err) {
//       started = false;
//       alert(err.message);
//     });
//   }

//   void _goHome() {
//     // Navigator.pushNamedAndRemoveUntil(context, HomePath, (Route<dynamic> route) => false);
//     Navigator.pushNamedAndRemoveUntil(
//         context, MeansPath, (Route<dynamic> route) => false);
//   }
// }
